﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WeatherHistory.Api.CommandsQueries.Commands.Forecasts.Add;
using WeatherHistory.Api.CommandsQueries.Commands.Forecasts.Delete;
using WeatherHistory.Api.CommandsQueries.Commands.Forecasts.Update;
using WeatherHistory.Api.CommandsQueries.Queries.Forecasts.GetOne;
using WeatherHistory.Api.Filters;
using WeatherHistory.Shared.Validation;

namespace WeatherHistory.Api.Controllers;

/// <summary>
///     Контроллер для работы с прогнозами погоды
/// </summary>
[ServiceFilter(typeof(AuthFilter))]
[Route("region/weather/forecast")]
public class ForecastController : ApiControllerBase
{
    /// <summary>Добавление прогноза погоды</summary>
    [HttpPost]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(typeof(AddForecastsCommandResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<AddForecastsCommandResponse>> Add([FromBody] AddForecastsCommandRequest request)
    {
        var result = await Mediator.Send(request);
        return Ok(result);
    }

    /// <summary>Удаление прогноза погоды</summary>
    [HttpDelete("{forecastId:long}")]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult> Delete([FromRoute] [GreaterThan(0L)] long forecastId)
    {
        var request = new DeleteForecastsCommandRequest();
        request.Id = forecastId;

        await Mediator.Send(request);
        return Ok();
    }

    /// <summary>Обновление прогноза погоды</summary>
    [HttpPut("{forecastId:long}")]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(typeof(UpdateForecastsCommandResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<UpdateForecastsCommandResponse>> Update([FromBody] UpdateForecastsCommandRequest request, [FromRoute] [GreaterThan(0L)] long forecastId)
    {
        request.ForecastId = forecastId;
        var result = await Mediator.Send(request);
        return Ok(result);
    }

    /// <summary>Получение информации о прогнозе погоды</summary>
    [HttpGet("{forecastId:long}")]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(typeof(GetOneForecastsQueryResponse), StatusCodes.Status200OK)]
    public async Task<ActionResult<GetOneForecastsQueryResponse>> GetOne([FromRoute] [GreaterThan(0L)] long forecastId)
    {
        var request = new GetOneForecastsQueryRequest();
        request.ForecastId = forecastId;

        var result = await Mediator.Send(request);
        return Ok(result);
    }
}